package com.ym.controller.patient.self;

import com.github.pagehelper.PageInfo;
import com.ym.common.constant.Constants;
import com.ym.core.annotation.ApiSign;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.shiro.ShiroUtil;
import com.ym.medical.domain.entity.InviteFriendMiddleEntity;
import com.ym.medical.domain.entity.UserEntity;
import com.ym.medical.domain.param.InviteFriendMiddleParam;
import com.ym.medical.domain.vo.MyCounsellingVo;
import com.ym.medical.service.ICommonSV;
import com.ym.medical.service.IInviteFriendMiddleSV;
import com.ym.spring.SessionContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的咨询
 */
@Api(tags = "患者端-个人中心")
@RestController
@ApiSign
@RequestMapping(value = "/patient/self/counselling", headers = Constants.Api.VERSION)
public class MyCounsellingController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(MyCounsellingController.class);

	@Autowired
	private ICommonSV commonSV;

	@Autowired
	private IInviteFriendMiddleSV inviteFriendMiddleSV;

	@Log(dataBase = false, printBody = false)
	@ApiOperation(value = "我的咨询-问诊记录")
	@GetMapping(value = "/patientRateTag")
	@ResponseBody
	public APIResponse<List<MyCounsellingVo>> queryMyCounselling(@RequestParam String type) {
		UserEntity user = SessionContext.getCurrUser();
		/*获取当前用户的id*/
		StringBuffer sql = new StringBuffer("SELECT " +
				"  b.id, " +
				" d.id ds_id, " +
				" g.head_url doct_photo_url, " +
				" g.real_name, " +
				" h.dept_name, " +
				" b.create_time, " +
				" b.illness_desc  " +
				"FROM " +
				" counselling_room a, " +
				" inquiry_order b, " +
				" studio_personnel c, " +
				" doctor_studio d, " +
				" doctor_details e, " +
				" hospital_dept f, " +
				" standard_dept h, " +
				" user g " +
				"WHERE " +
				" a.ds_id = c.id  " +
				" AND a.io_id = b.id  " +
				" and  f.hos_code_uni = e.hospital_code" +
				" AND c.ds_id = d.id  " +
				" AND f.stdept_code_uni = h.dept_code  " +
				" AND d.belong_person = e.doct_id  " +
				" AND e.dept_code = f.dept_code  " +
				" AND e.doct_id = g.id " +
				" and b.user_id=#{params.user_id} ");
		if ("01".equals(type)) {
			sql.append("and a.cour_state in ('01','02')");
		} else {
			sql.append("and a.cour_state in ('03')");
		}
		Map<String, Object> params = new HashMap<>();
		params.put("user_id", user.getId());
		List<MyCounsellingVo> list = commonSV.queryListEntity(sql.toString(), params, MyCounsellingVo.class);
		return successData(list);
	}

	@ApiOperation("邀请好友记录")
	@GetMapping(value = "/listInvite")
	public APIResponse<PageInfo<InviteFriendMiddleEntity>> listInvite(InviteFriendMiddleParam model,
																	  @RequestParam(defaultValue = "1", required = false) int pageNum,
																	  @RequestParam(defaultValue = "5", required = false) int pageSize) {
		model.setInviteId(ShiroUtil.getLoginId());
		return successData(inviteFriendMiddleSV.listInvite(model, pageNum, pageSize));
	}
}
